package com.foolishbird.sdk.params;



import com.foolishbird.sdk.secret.MD5;
import com.foolishbird.sdk.utils.WL;
import com.foolishbird.sdk.utils.XmlUtils;

import org.json.JSONException;
import org.json.JSONObject;



public final class PostRequestParams {

    private static final String TAG = PostRequestParams.class.getSimpleName();

//    final String service;

    final String sign;

//    final long time;

    final JSONObject data;

    private PostRequestParams(final Builder builder) {
//        this.time = builder.time;
        this.sign = builder.sign;
//        this.service = builder.service;
        this.data = builder.data;
    }

    public static PostRequestParams createRequestParams(RequestDataParams data) {

        return new Builder(data).build();
    }

    public String toParamsString() {
        WL.e(TAG, "toParamsString ---");
//        JSONObject jsonStr = new JSONObject();
//        try {
//            jsonStr.put("data", data);
////            jsonStr.put("service", service);
//            jsonStr.put("sign", sign);
////            jsonStr.put("time", time);
//        } catch (JSONException e) {
//            WL.e(TAG, "toParamsString ---> JSONException");
//        }

        try {
            data.getJSONObject("base_fb").getJSONObject("validation_info").put("sign",sign);
        } catch (JSONException e) {
            e.printStackTrace();
        }


        WL.e(TAG, "toParamsString ---"+ data.toString());



        return data.toString();
    }

    public static class Builder {
//        private String service;
        private String sign;
//        private long time;
        private JSONObject data;

        public Builder(RequestDataParams data) {
//            this.service = data.getServiceString();
            this.data = data.toJsonObject();
            WL.d(TAG, "data to string: " + data.toString());
//            this.time = System.currentTimeMillis() / 1000;
            StringBuilder builder = new StringBuilder()
//                    .append(service)
//                    .append(time)
                    .append(data.toString())
                    .append("key="+XmlUtils.getPlatformSecret());
            WL.d(TAG, "Builder sign string: " + builder.toString());
            this.sign = MD5.encrypt(builder.toString()).toLowerCase();
        }
        public PostRequestParams build() {
            return new PostRequestParams(this);
        }

    }
}
